extends Area2D

@export var speed: float = 60.0
@onready var anim: AnimatedSprite2D = get_node("AnimatedSprite2D")
@onready var is_dead: bool = false
@onready var death_audio: AudioStreamPlayer = get_node("AudioStreamPlayer_EnemyDeath")


func _ready() -> void:
	pass

func _physics_process(delta: float) -> void:
	if is_dead:
		return
	position += Vector2(-1, 0) * speed * delta
	if position.x < -270:
		MessageCenter.send(self, "score_changed", -5)
		queue_free()


func _on_body_entered(body: Node2D) -> void:
	if body is CharacterBody2D and not is_dead:
		body.onGameOver()


func _on_area_entered(area: Area2D) -> void:
	if area.is_in_group("bullet") and not is_dead:
		#print("Bullet entered")
		anim.play("die")
		is_dead = true
		area.queue_free()
		MessageCenter.send(self, "score_changed", 1)
		death_audio.play()
		#print("send score_changed")

		await get_tree().create_timer(0.6).timeout
		queue_free()
